﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Globalization;

namespace Web.Controllers
{
    /// <summary>
    /// 参数检查器
    /// </summary>
    public abstract partial class JsonController : Controller
    {
        public void CheckNull(params object[] objs)
        {
            foreach (var obj in objs)
                if (obj == null)
                {
                    TempData["json"] = new
                    {
                        success = false,
                        msg = "参数不够"
                    };
                    TempData["IsFileUpload"] = IsFileUpload;
                    Response.Redirect(Url.Action("json", "system"));
                }
        }
        public void CheckEmpty(params object[] objs)
        {
            foreach (var obj in objs)
                if (obj == null || obj.ToString() == "")
                {
                    TempData["json"] = new
                    {
                        success = false,
                        msg = "参数不能为空"
                    };
                    TempData["IsFileUpload"] = IsFileUpload;
                    Response.Redirect(Url.Action("json", "system"));
                }
        }
        public void CheckWhite(params object[] objs)
        {
            foreach (var obj in objs)
                if (obj == null || string.IsNullOrWhiteSpace(obj.ToString()))
                {
                    TempData["json"] = new
                    {
                        success = false,
                        msg = "参数不能为空"
                    };
                    Response.Redirect(Url.Action("json", "system"));
                }
        }
        public void CheckHtmlWhite(params object[] objs)
        {
            foreach (var obj in objs)
                if (obj == null || string.IsNullOrWhiteSpace(Core.functional.stripHtml(obj.ToString())))
                {
                    TempData["json"] = new
                    {
                        success = false,
                        msg = "参数不能为空"
                    };
                    Response.Redirect(Url.Action("json", "system"));
                }
        }

        public void CheckMaxLen(int maxlen, params object[] objs)
        {
            foreach (var obj in objs)
                if (obj != null && obj.ToString().Length > maxlen)
                {
                    TempData["json"] = new
                    {
                        success = false,
                        msg = "参数过长"
                    };
                    Response.Redirect(Url.Action("json", "system"));
                }
        }

        public DateTime? CheckDateQ(string date)
        {
            if (string.IsNullOrWhiteSpace(date)) return null;
            DateTime resultdate;
            if (!DateTime.TryParse(date, out resultdate))
            {
                TempData["json"] = new
                {
                    success = false,
                    msg = "日期参数格式错误"
                };
                Response.Redirect(Url.Action("json", "system"));
            }
            return resultdate;
        }

        public DateTime CheckDate(string date)
        {
            var resultdate = new DateTime();
            if (string.IsNullOrWhiteSpace(date) || !DateTime.TryParse(date, out resultdate))
            {
                TempData["json"] = new
                {
                    success = false,
                    msg = "日期参数格式错误"
                };
                Response.Redirect(Url.Action("json", "system"));
            }
            return resultdate;
        }
    }
}